<?php

    include("conn.php");

    $t1 = microtime(true);

    $uid=$_REQUEST ["uid"]; ///  uid自己。 以后要换token _GET  _POST

    $fid=$_REQUEST ["fid"]; 

	 

//是否已经是好友了。
function is_friend( $uid , $fid )
{ 

    $strsql="select * from tb_friend where uid=$uid and fid=$fid "; 

	// 执行sql查询
	$result=mysql_query($strsql, $conn);
	  
	$rows=mysql_fetch_assoc($result);
	if(!$rows)
	{ 
		//echo "不存在 ";
         
		return false; 
	}

    return true;
}
	
	//删除好友 相互列表都删对方
	//http://192.168.1.100/s/delfriend.php?uid=22&fid=23 

    

    //参数验证
    if($uid==NULL || $fid==NULL   ){
        $ret->code=-1; 
		$ret->desc="no param"; 
		die( json_encode($ret) );  
    }

	//还要验证好友关系。。。
	if(is_friend($uid,$fid)==false)
	{
		$ret->code=0; 
		$ret->desc=" not   friend"; 
		die( json_encode($ret) );  
	}
 
	
		//tb_friend  uid fid
	 
		$jb->code=0;
		
		// 解除好友关联。
		$s2="delete from tb_friend where (uid=$uid and fid=$fid) or (fid =$uid and uid =$fid) ";
		
		//echo $s2; 

		//执行
		mysql_query($s2, $conn)  or die(  json_encode($jb) );

		//删除2人聊天记录
		$s2="delete from tb_friendmsg where send=$uid and _to=$fid ";
		mysql_query($s2, $conn)  or die(  json_encode($jb) );
		
		$s2="delete from tb_friendmsg where _to=$uid and send=$fid ";
		mysql_query($s2, $conn)  or die(  json_encode($jb) );

		//session 信息相互删除。
		$s2="delete from tb_session where uid=$uid and friendID=$fid ";
		mysql_query($s2, $conn)  or die(  json_encode($jb) );
		
		$s2="delete from tb_session where friendID=$uid and uid=$fid ";
		mysql_query($s2, $conn)  or die(  json_encode($jb) );

	 

		mysql_close();

        // echo "   $s2" , mysql_affected_rows() ;

           
        $t2 = microtime(true);
     
		$jb->code=1;
        $jb->test=  "ok ,耗时".round($t2-$t1,6)."秒"; 
        echo json_encode($jb);

 

		
	//还要更新对方的会话表。插入or更新。 
	$strsql="select * from tb_session where uid=$to and friendID=$from  "; 

	// 执行sql查询
	$result=mysql_query($strsql, $conn);
	   
	$rows=mysql_fetch_assoc($result);
	if(!$rows)
	{
		 
			//不存在，
		$strsql="insert  into tb_session (uid , friendID , lastContent , lastTime ,state，type ) values( $to ,  $from , '$content' ,now() , -1 ,2) "; 

		// echo $strsql;

		mysql_query($strsql, $conn);//   or die(  json_encode($jb) );
		 
	}


?>
